#include "task_struct.h"

void test1()
{
	task_struct* t1 = Buy_PCB("xuyi", 18);
	task_struct* t2 = Buy_PCB("zhaoer", 19);
	task_struct* t3 = Buy_PCB("zhangsan", 20);
	task_struct* t4 = Buy_PCB("lisi", 21);
	task_struct* t5 = Buy_PCB("wangwu", 22);

	LTNode* runhead = LTCreate();
	runPushBack(runhead, t1);
	runPushBack(runhead, t2);
	runPushBack(runhead, t3);
	runPushBack(runhead, t4);
	runPushBack(runhead, t5);
	runPopBack(runhead);
	LTNode* pos = runFind_by_name(runhead, "zhangsan");
	runErase(runhead, pos);
	runPrint_name(runhead);
	runPrint_type(runhead);

	LTNode* waithead = LTCreate();
	waitPushFront(waithead, t1);
	waitPushFront(waithead, t2);
	waitPushFront(waithead, t3);
	waitPushFront(waithead, t4);
	waitPushFront(waithead, t5);
	waitPrint_name(waithead);
	waitPrint_type(waithead);
	waitPopFront(waithead);
	waitPopFront(waithead);
	waitPopFront(waithead);
	waitPopFront(waithead);
	waitPopFront(waithead);
	waitPrint_name(waithead);
	waitPrint_type(waithead);

    LTDestroy(runhead);
    LTDestroy(waithead);
    Destroy_PCB(t1);
    Destroy_PCB(t2);
    Destroy_PCB(t3);
    Destroy_PCB(t4);
    Destroy_PCB(t5);
}

void test_queue()
{
    int num = 10;
    task_struct* t1 = Buy_PCB("a", 1);
    task_struct* t2 = Buy_PCB("a", 2);
    task_struct* t3 = Buy_PCB("a", 3);
    task_struct* t4 = Buy_PCB("a", 4);
    task_struct* t5 = Buy_PCB("a", 5);
    task_struct* t6 = Buy_PCB("a", 6);
    task_struct* t7 = Buy_PCB("a", 7);
    task_struct* t8 = Buy_PCB("a", 8);
    task_struct* t9 = Buy_PCB("a", 9);
    task_struct* t10 = Buy_PCB("a", 10);
    LTNode* runhead = LTCreate();
    LTNode* waithead = LTCreate();
    runPushBack(runhead, t1);
    runPushBack(runhead, t2);
    runPushBack(runhead, t3);
    runPushBack(runhead, t4);
    runPushBack(runhead, t5);
    runPushBack(runhead, t6);
    runPushBack(runhead, t7);
    runPushBack(runhead, t8);
    runPushBack(runhead, t9);
    runPushBack(runhead, t10);
    
    while(!LTEmpty(runhead) || !LTEmpty(waithead))
    {
        runPrint_type(runhead);
        waitPrint_type(waithead);

        sleep(1);
        if(!LTEmpty(waithead))
        {
            runPushBack(runhead, waitQueueFront(waithead));
            waitPopFront(waithead);
        }
        if(num > 0)
        {
            waitPushBack(waithead, runQueueFront(runhead));
            --num;
        }
        runPopFront(runhead);
    }
    LTDestroy(runhead);
    LTDestroy(waithead);
    
    Destroy_PCB(t1);
    Destroy_PCB(t2);
    Destroy_PCB(t3);
    Destroy_PCB(t4);
    Destroy_PCB(t5);
    Destroy_PCB(t6);
    Destroy_PCB(t7);
    Destroy_PCB(t8);
    Destroy_PCB(t9);
    Destroy_PCB(t10);
}

int main()
{
	test_queue();
	return 0;
}
